<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xpcdemo.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_demos_xpc_xpcdemo.js.html">xpcdemo.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Contains application code for the XPC demo.
<a name="line17"></a> * This script is used in both the container page and the iframe.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line22"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line26"></a>goog.require(&#39;goog.json&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.xpc.CrossPageChannel&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Namespace for the demo. We don&#39;t use goog.provide here because it&#39;s not a
<a name="line32"></a> * real module (cannot be required).
<a name="line33"></a> */
<a name="line34"></a>var xpcdemo = {};
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Global function to kick off initialization in the containing document.
<a name="line39"></a> */
<a name="line40"></a>goog.global.initOuter = function() {
<a name="line41"></a>  goog.events.listen(window, &#39;load&#39;, function() { xpcdemo.initOuter(); });
<a name="line42"></a>};
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Global function to kick off initialization in the iframe.
<a name="line47"></a> */
<a name="line48"></a>goog.global.initInner = function() {
<a name="line49"></a>  goog.events.listen(window, &#39;load&#39;, function() { xpcdemo.initInner(); });
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Initializes XPC in the containing page.
<a name="line55"></a> */
<a name="line56"></a>xpcdemo.initOuter = function() {
<a name="line57"></a>  // Build the configuration object.
<a name="line58"></a>  var cfg = {};
<a name="line59"></a>
<a name="line60"></a>  var ownUri = new goog.Uri(window.location.href);
<a name="line61"></a>  var relayUri = ownUri.resolve(new goog.Uri(&#39;relay.html&#39;));
<a name="line62"></a>  var pollUri = ownUri.resolve(new goog.Uri(&#39;blank.html&#39;));
<a name="line63"></a>
<a name="line64"></a>  // Determine the peer domain. Uses the value of the URI-parameter
<a name="line65"></a>  // &#39;peerdomain&#39;. If that parameter is not present, it falls back to
<a name="line66"></a>  // the own domain so that the demo will work out of the box (but
<a name="line67"></a>  // communication will of course not cross domain-boundaries).  For
<a name="line68"></a>  // real cross-domain communication, the easiest way is to point two
<a name="line69"></a>  // different host-names to the same webserver and then hit the
<a name="line70"></a>  // following URI:
<a name="line71"></a>  // http://host1.com/path/to/closure/demos/xpc/index.html?peerdomain=host2.com
<a name="line72"></a>  var peerDomain = ownUri.getParameterValue(&#39;peerdomain&#39;) || ownUri.getDomain();
<a name="line73"></a>
<a name="line74"></a>  cfg[goog.net.xpc.CfgFields.LOCAL_RELAY_URI] = relayUri.toString();
<a name="line75"></a>  cfg[goog.net.xpc.CfgFields.PEER_RELAY_URI] =
<a name="line76"></a>      relayUri.setDomain(peerDomain).toString();
<a name="line77"></a>
<a name="line78"></a>  cfg[goog.net.xpc.CfgFields.LOCAL_POLL_URI] = pollUri.toString();
<a name="line79"></a>  cfg[goog.net.xpc.CfgFields.PEER_POLL_URI] =
<a name="line80"></a>      pollUri.setDomain(peerDomain).toString();
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>  // Force transport to be used if tp-parameter is set.
<a name="line84"></a>  var tp = ownUri.getParameterValue(&#39;tp&#39;);
<a name="line85"></a>  if (tp) {
<a name="line86"></a>    cfg[goog.net.xpc.CfgFields.TRANSPORT] = parseInt(tp, 10);
<a name="line87"></a>  }
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>  // Construct the URI of the peer page.
<a name="line91"></a>
<a name="line92"></a>  var peerUri = ownUri.resolve(
<a name="line93"></a>      new goog.Uri(&#39;inner.html&#39;)).setDomain(peerDomain);
<a name="line94"></a>  // Passthrough of verbose and compiled flags.
<a name="line95"></a>  if (goog.isDef(ownUri.getParameterValue(&#39;verbose&#39;))) {
<a name="line96"></a>    peerUri.setParameterValue(&#39;verbose&#39;, &#39;&#39;);
<a name="line97"></a>  }
<a name="line98"></a>  if (goog.isDef(ownUri.getParameterValue(&#39;compiled&#39;))) {
<a name="line99"></a>    peerUri.setParameterValue(&#39;compiled&#39;, &#39;&#39;);
<a name="line100"></a>  }
<a name="line101"></a>
<a name="line102"></a>  cfg[goog.net.xpc.CfgFields.PEER_URI] = peerUri;
<a name="line103"></a>
<a name="line104"></a>  // Instantiate the channel.
<a name="line105"></a>  xpcdemo.channel = new goog.net.xpc.CrossPageChannel(cfg);
<a name="line106"></a>
<a name="line107"></a>  // Create the peer iframe.
<a name="line108"></a>  xpcdemo.peerIframe = xpcdemo.channel.createPeerIframe(
<a name="line109"></a>      goog.dom.getElement(&#39;iframeContainer&#39;));
<a name="line110"></a>
<a name="line111"></a>  xpcdemo.initCommon_();
<a name="line112"></a>
<a name="line113"></a>  goog.dom.getElement(&#39;inactive&#39;).style.display = &#39;none&#39;;
<a name="line114"></a>  goog.dom.getElement(&#39;active&#39;).style.display = &#39;&#39;;
<a name="line115"></a>};
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Initialization in the iframe.
<a name="line120"></a> */
<a name="line121"></a>xpcdemo.initInner = function() {
<a name="line122"></a>  // Get the channel configuration passed by the containing document.
<a name="line123"></a>  var cfg = goog.json.parse(
<a name="line124"></a>      (new goog.Uri(window.location.href)).getParameterValue(&#39;xpc&#39;));
<a name="line125"></a>
<a name="line126"></a>  xpcdemo.channel = new goog.net.xpc.CrossPageChannel(cfg);
<a name="line127"></a>
<a name="line128"></a>  xpcdemo.initCommon_();
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Initializes the demo.
<a name="line134"></a> * Registers service-handlers and connects the channel.
<a name="line135"></a> * @private
<a name="line136"></a> */
<a name="line137"></a>xpcdemo.initCommon_ = function() {
<a name="line138"></a>  var xpcLogger = goog.debug.Logger.getLogger(&#39;goog.net.xpc&#39;);
<a name="line139"></a>  xpcLogger.addHandler(function(logRecord) {
<a name="line140"></a>    xpcdemo.log(&#39;[XPC] &#39; + logRecord.getMessage());
<a name="line141"></a>  });
<a name="line142"></a>  xpcLogger.setLevel(window.location.href.match(/verbose/) ?
<a name="line143"></a>      goog.debug.Logger.Level.ALL : goog.debug.Logger.Level.INFO);
<a name="line144"></a>
<a name="line145"></a>  // Register services.
<a name="line146"></a>  xpcdemo.channel.registerService(&#39;log&#39;, xpcdemo.log);
<a name="line147"></a>  xpcdemo.channel.registerService(&#39;ping&#39;, xpcdemo.pingHandler_);
<a name="line148"></a>  xpcdemo.channel.registerService(&#39;events&#39;, xpcdemo.eventsMsgHandler_);
<a name="line149"></a>
<a name="line150"></a>  // Connect the channel.
<a name="line151"></a>  xpcdemo.channel.connect(function() {
<a name="line152"></a>    xpcdemo.channel.send(&#39;log&#39;, &#39;Hi from &#39; + window.location.host);
<a name="line153"></a>    goog.events.listen(goog.dom.getElement(&#39;clickfwd&#39;),
<a name="line154"></a>                       &#39;click&#39;, xpcdemo.mouseEventHandler_);
<a name="line155"></a>  });
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Kills the peer iframe and the disposes the channel.
<a name="line161"></a> */
<a name="line162"></a>xpcdemo.teardown = function() {
<a name="line163"></a>  goog.events.unlisten(goog.dom.getElement(&#39;clickfwd&#39;),
<a name="line164"></a>                       goog.events.EventType.CLICK, xpcdemo.mouseEventHandler_);
<a name="line165"></a>
<a name="line166"></a>  xpcdemo.channel.dispose();
<a name="line167"></a>  delete xpcdemo.channel;
<a name="line168"></a>
<a name="line169"></a>  goog.dom.removeNode(xpcdemo.peerIframe);
<a name="line170"></a>  xpcdemo.peerIframe = null;
<a name="line171"></a>
<a name="line172"></a>  goog.dom.getElement(&#39;inactive&#39;).style.display = &#39;&#39;;
<a name="line173"></a>  goog.dom.getElement(&#39;active&#39;).style.display = &#39;none&#39;;
<a name="line174"></a>};
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/**
<a name="line178"></a> * Logging function. Inserts log-message into element with it id &#39;console&#39;.
<a name="line179"></a> * @param {string} msgString The log-message.
<a name="line180"></a> */
<a name="line181"></a>xpcdemo.log = function(msgString) {
<a name="line182"></a>  xpcdemo.consoleElm || (xpcdemo.consoleElm = goog.dom.getElement(&#39;console&#39;));
<a name="line183"></a>  var msgElm = goog.dom.createDom(&#39;div&#39;);
<a name="line184"></a>  msgElm.innerHTML = msgString;
<a name="line185"></a>  xpcdemo.consoleElm.insertBefore(msgElm, xpcdemo.consoleElm.firstChild);
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Sends a ping request to the peer.
<a name="line191"></a> */
<a name="line192"></a>xpcdemo.ping = function() {
<a name="line193"></a>  // send current time
<a name="line194"></a>  xpcdemo.channel.send(&#39;ping&#39;, goog.now() + &#39;&#39;);
<a name="line195"></a>};
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * The handler function for incoming pings (messages sent to the service
<a name="line199"></a> * called &#39;ping&#39;);
<a name="line200"></a> * @param {string} payload The message payload.
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>xpcdemo.pingHandler_ = function(payload) {
<a name="line204"></a>  // is the incoming message a response to a ping we sent?
<a name="line205"></a>  if (payload.charAt(0) == &#39;#&#39;) {
<a name="line206"></a>    // calculate roundtrip time and log
<a name="line207"></a>    var dt = goog.now() - parseInt(payload.substring(1), 10);
<a name="line208"></a>    xpcdemo.log(&#39;roundtrip: &#39; + dt + &#39;ms&#39;);
<a name="line209"></a>  } else {
<a name="line210"></a>    // incoming message is a ping initiated from peer
<a name="line211"></a>    // -&gt; prepend with &#39;#&#39; and send back
<a name="line212"></a>    xpcdemo.channel.send(&#39;ping&#39;, &#39;#&#39; + payload);
<a name="line213"></a>    xpcdemo.log(&#39;ping reply sent&#39;);
<a name="line214"></a>  }
<a name="line215"></a>};
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/**
<a name="line219"></a> * Counter for mousemove events.
<a name="line220"></a> * @type {number}
<a name="line221"></a> * @private
<a name="line222"></a> */
<a name="line223"></a>xpcdemo.mmCount_ = 0;
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * Holds timestamp when the last mousemove rate has been logged.
<a name="line228"></a> * @type {number}
<a name="line229"></a> * @private
<a name="line230"></a> */
<a name="line231"></a>xpcdemo.mmLastRateOutput_ = 0;
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Start mousemove event forwarding. Registers a listener on the document which
<a name="line236"></a> * sends them over the channel.
<a name="line237"></a> */
<a name="line238"></a>xpcdemo.startMousemoveForwarding = function() {
<a name="line239"></a>  goog.events.listen(document, goog.events.EventType.MOUSEMOVE,
<a name="line240"></a>                     xpcdemo.mouseEventHandler_);
<a name="line241"></a>  xpcdemo.mmLastRateOutput_ = goog.now();
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * Stop mousemove event forwarding.
<a name="line247"></a> */
<a name="line248"></a>xpcdemo.stopMousemoveForwarding = function() {
<a name="line249"></a>  goog.events.unlisten(document, goog.events.EventType.MOUSEMOVE,
<a name="line250"></a>                       xpcdemo.mouseEventHandler_);
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Function to be used as handler for mouse-events.
<a name="line256"></a> * @param {goog.events.BrowserEvent} e The mouse event.
<a name="line257"></a> * @private
<a name="line258"></a> */
<a name="line259"></a>xpcdemo.mouseEventHandler_ = function(e) {
<a name="line260"></a>  xpcdemo.channel.send(&#39;events&#39;,
<a name="line261"></a>                   [e.type, e.clientX, e.clientY, goog.now()].join(&#39;,&#39;));
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Handler for the &#39;events&#39; service.
<a name="line267"></a> * @param {string} payload The string returned from the xpcdemo.
<a name="line268"></a> * @private
<a name="line269"></a> */
<a name="line270"></a>xpcdemo.eventsMsgHandler_ = function(payload) {
<a name="line271"></a>  var now = goog.now();
<a name="line272"></a>  var args = payload.split(&#39;,&#39;);
<a name="line273"></a>  var type = args[0];
<a name="line274"></a>  var pageX = args[1];
<a name="line275"></a>  var pageY = args[2];
<a name="line276"></a>  var time = parseInt(args[3], 10);
<a name="line277"></a>
<a name="line278"></a>  var msg = type + &#39;: (&#39; + pageX + &#39;,&#39; + pageY + &#39;), latency: &#39; + (now - time);
<a name="line279"></a>  xpcdemo.log(msg);
<a name="line280"></a>
<a name="line281"></a>  if (type == goog.events.EventType.MOUSEMOVE) {
<a name="line282"></a>    xpcdemo.mmCount_++;
<a name="line283"></a>    var dt = now - xpcdemo.mmLastRateOutput_;
<a name="line284"></a>    if (dt &gt; 1000) {
<a name="line285"></a>      msg = &#39;RATE (mousemove/s): &#39; + (1000 * xpcdemo.mmCount_ / dt);
<a name="line286"></a>      xpcdemo.log(msg);
<a name="line287"></a>      xpcdemo.mmLastRateOutput_ = now;
<a name="line288"></a>      xpcdemo.mmCount_ = 0;
<a name="line289"></a>    }
<a name="line290"></a>  }
<a name="line291"></a>};
<a name="line292"></a>
<a name="line293"></a>
<a name="line294"></a>/**
<a name="line295"></a> * Send multiple messages.
<a name="line296"></a> * @param {number} n The number of messages to send.
<a name="line297"></a> */
<a name="line298"></a>xpcdemo.sendN = function(n) {
<a name="line299"></a>  xpcdemo.count_ || (xpcdemo.count_ = 1);
<a name="line300"></a>
<a name="line301"></a>  for (var i = 0; i &lt; n; i++) {
<a name="line302"></a>    xpcdemo.channel.send(&#39;log&#39;, &#39;&#39; + xpcdemo.count_++);
<a name="line303"></a>  }
<a name="line304"></a>};
</pre>


</body>
</html>
